import { setFormatteMenu } from '@/Ulits/formateMenu'
import router from '@/router'
import { RequestMenuList } from '@/api/My'

export const Menu = {
	state: {
		menuList: [] //菜单数组
	},
	mutations: {
		SAVE_MENU(state, menuList) {
			state.menuList = menuList
		},
		CLEAR_MENU(state) {
			state.menuList = []
		}
	},
	actions: {
		saveMenu({ commit }, roleId) {
			// menuList 根据登录用户角色调用菜单接口返回

			RequestMenuList(roleId).then(data => {
				if (data.code == 200) {
					let routesMenu = setFormatteMenu(data.sysmenu)

					commit('CLEAR_MENU') // 先清除原来菜单数据
					commit('SAVE_MENU', routesMenu)
					router.addRoutes(routesMenu) // 动态添加路由
				}
			})
		},
		clearMenu({ commit }) {
			commit('CLEAR_MENU')
		}
	}
}